<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use app\index\model\Student as StudentModel;
use think\Session;
use think\Request;
use think\Validate;
use think\Loader;


class Student extends Controller
{
	public function selectAll()
	{
		Config::set('APP_DEBUG',false);
		$a=['php','great'];
		//echo $a;
		echo function_exists("print_r");
		$f="";
		echo IS_string($f);
		 $list =StudentModel::hasWhere("classes",["mr_classname"=>["like","%P%"]])->select();//->where("s_id",">",15)
		// 打印SQL
		//echo $list =StudentModel::hasWhere("classes",["mr_classname"=>"PHP1班"])->buildSql();//->where("s_id",">",15)
		// echo $student->classes->mr_classname;
		return $this->fetch("",['list'=>$list]);
	}
	
	public function upload(){
		
		//echo ROOT_PATH."<br/>";

		 //$_FILES['myphoto']['name'] //size type tmp_name error
		$request = Request::instance();
		$file=$request->file("myphoto")->move(ROOT_PATH."public/uploads");//->validate(['size'=>1024*1024,'ext'=>'jpg,png,gif'])
		return $this->fetch("insert",["savename"=>$file->getSaveName()]);
		//var_dump($file);
		if($file){
			
		}else{
			echo "失败";
		}
	}
	
	public function insert()
	{
		return $this->fetch();
	}
	
	public function select()
	{
		//定义字符串类型的变量
		$str = "今天有老师来听课。";
		//定义个Unix时间戳类型的变量
		$date = time();//整型值
		
		$arr = [];//定义变量时要赋初值。
		$nickname=20;
		
		//定义变量 a b 
		$a=10;
		$b=20;
		//跳转到一个（渲染）视图
		return $this->fetch("",[
				"str" => $str,
				"date" => $date,
				"nickname"=>$nickname,
				"a"=>$a,
				"b"=>$b,
		]);
	}
	
	public function login()
	{
		Session::set("name","毕鹏婕");
	}
	
	public function val()
	{
		//定义规则
		/*$validate = new Validate([
			"name" => "require",
			"email" => "email",
		]);
		
		$data=[
			"name" => '前端',
			"email"=>"460153585@qq.com",	
		];
		if(!$validate->check($data)){
			var_dump($validate->getError());
		}else{
			echo "验证成功";
		}
		*/
		
		$data=[
				"email"=>"460153585@qq.com",
				"age" =>"18.5",
		];
		//得到validate类的实例
		$validate = Loader::validate("Student");//定义根据哪个规则去处理
		if(!$validate->check($data)){//->scene('add')
			//var_dump($validate->getError());
			$this->assign("error",$validate->getError());
			return $this->fetch("val");
		}else{
			echo "验证成功";
		}
		
		
	}
	
	
	
}